angular.module('mamApp').controller('specialTagsController', ['$scope', '$state', '$uibModal','ImsDialog','TagsManager','$filter',
    function ($scope, $state, $uibModal, ImsDialog, TagsManager, $filter) {

    $scope.search = {
        pageNo: $state.params.pageNo || 1,
        pageSize: $state.params.pageSize || 10,
        channelId: $state.params.channelId
    };

    $scope.paginationConf = {
        currentPage: $state.params.pageNo || 1,
        itemsPerPage: $state.params.pageSize || 10,
        pagesLength: 9,
        search: 0,
        onChange: function () {
            $scope.search.pageNo = this.currentPage;
            $scope.search.pageSize = this.itemsPerPage;
            $state.go($state.current.name, $scope.search)
        }
    };

    //专用标签维度列表
    $scope.noPermission=false;
    TagsManager.specialDimensions([$scope.search.channelId,$scope.search.pageNo, $scope.search.pageSize], $scope.search).then(function (result) {

        if(result.data.c=="n"){
            $scope.noPermission=true;
        }else{
            $scope.noPermission=false;
            $scope.specialDimensions = result.data.pageList;
            $scope.paginationConf.totalItems = result.data.total;
        }


    });

    //初始化全选值为false
    $scope.checkData={allChecked:false};
    //全选
    $scope.selectAllChange=function (checked) {
        $scope.specialDimensions.forEach(function (item) {
            item.checked=checked;
        })
    };
    //选择一个
    $scope.selectOneChange=function (checked) {
        if(!checked){
            $scope.checkData.allChecked=false;
        }else {
            ($filter('filter')($scope.specialDimensions,{checked:true}).length)==$scope.specialDimensions.length?$scope.checkData.allChecked=true:$scope.checkData.allChecked=false;
        }
    };

    //获取新建/编辑里面“所属频道列表”
    TagsManager.specialCreateChannelList().then(function (result) {
        $scope.specialCreateChannelList = result.data.pageList;
    });

    //新建数据
    $scope.create = function () {
        $scope.formData = {};
        $scope.formData.status = '2';
        $scope.formData.channelId=$state.params.channelId;
        var modalInstance = $uibModal.open({
            animation: true,
            size: 'normal',
            openedClass:"modal-open-normal",
            templateUrl: 'specialDimensionModal.html',
            controller: 'specialDimensionModalController',
            resolve: {
                data:function () {
                    return angular.copy($scope.formData);
                },
                channelList:function () {
                    return angular.copy($scope.specialCreateChannelList);
                }
            }
        });
        modalInstance.result.then(function (result) {
            if (result=="ok"){
                $state.reload();
            }
/*            if (result=="error"){
                ImsDialog.open({title:"专用标签管理",message:"维度名称已存在。"}).then(function (a) {})
            }*/
        })
    };

    //编辑数据
    $scope.edit = function (item) {
        $scope.formData = {};
        $scope.formData.dimensionId = item.dimensionId;
        $scope.formData.dimensionsName = item.dimensionsName;
        $scope.formData.status = item.status;
        var modalInstance = $uibModal.open({
            animation: true,
            size: 'normal',
            openedClass:"modal-open-normal",
            templateUrl: 'specialDimensionModal.html',
            controller: 'specialDimensionModalController',
            resolve: {
                data:function () {
                    return angular.copy($scope.formData);
                },
                channelList:function () {
                    return angular.copy($scope.specialCreateChannelList);
                }
            }
        });
        modalInstance.result.then(function (result) {
            if (result=="ok"){
                $state.reload();
            }
/*            if (result=="error"){
                ImsDialog.open({title:"专用标签管理",message:"维度名称已存在。"}).then(function (a) {})
            }*/
        })
    };

    var statusChange = {};

    //禁用一个
    $scope.disable=function (item) {
        statusChange.status = '2';
        ImsDialog.open({title:"专用标签管理",message:'确定禁用该标签维度？<br><small style="color: #CCCCCC">注：禁用维度后，将限制新专辑对禁用标签的使用，但不影响之前的专辑</small>'}).then(function (a) {
            if(a=="ok"){
                TagsManager.specialDimensionsChangeStatus(item.dimensionId,statusChange).then(function (result) {
                    if(result.data.c=="s"){
                        $state.reload();
                    }
                })
            }
        })
    };

    //启用一个
    $scope.enable=function (item) {
        statusChange.status = '4';
        ImsDialog.open({title:"专用标签管理",message:'确定启用该标签维度？'}).then(function (a) {
            if(a=="ok"){
                TagsManager.specialDimensionsChangeStatus(item.dimensionId,statusChange).then(function (result) {
                    if(result.data.c=="s"){
                        $state.reload();
                    }
                })
            }
        })
    };

    //禁用多个
    $scope.disableAll=function () {
        var tempArray = [];
        $scope.specialDimensions.forEach(function (item) {
            if(item.checked == true){
                if(item.status == '4'){
                    tempArray.push(item.dimensionId)
                }
            }
        });
        statusChange.status = '2';
        if(tempArray.length>0){
            ImsDialog.open({title:"专用标签管理",message:'确定禁用该标签维度？<br><small style="color: #CCCCCC">注：禁用维度后，将限制新专辑对禁用标签的使用，但不影响之前的专辑</small>'}).then(function (a) {
                if(a=="ok"){
                    TagsManager.specialDimensionsChangeStatus(tempArray,statusChange).then(function (result) {
                        if(result.data.c=="s"){
                            $state.reload();
                        }
                    })
                }
            })
        } else {
            return false;
            //ImsDialog.open({title:"专用标签管理",message:"没有可禁用的数据。"}).then(function (a) {})
        }
    };

    //启用多个
    $scope.enableAll=function () {
        var tempArray = [];
        $scope.specialDimensions.forEach(function (item) {
            if(item.checked == true){
                if(item.status == '2'){
                    tempArray.push(item.dimensionId)
                }
            }
        });
        statusChange.status = '4';
        if(tempArray.length>0){
            ImsDialog.open({title:"专用标签管理",message:'确定启用该标签维度？'}).then(function (a) {
                if(a=="ok"){
                    TagsManager.specialDimensionsChangeStatus(tempArray,statusChange).then(function (result) {
                        if(result.data.c=="s"){
                            $state.reload();
                        }
                    })
                }
            })
        } else {
            return false;
            //ImsDialog.open({title:"专用标签管理",message:"没有可启用的数据。"}).then(function (a) {})
        }
    };

    //删除一个数据
    $scope.delete=function (item) {
        ImsDialog.open({title:"专用标签管理",message:'确定删除该标签维度？'}).then(function (a) {
            if(a=="ok"){
                TagsManager.specialDimensionsDelete(item.dimensionId).then(function (result) {
                    if(result.data.c=="s"){
                        $state.reload();
                    }
                })
            }
        })
    };

    //删除多个数据
    $scope.deleteAll=function () {
        var tempArray = [];
        $scope.specialDimensions.forEach(function (item) {
            if(item.checked == true){
                tempArray.push(item.dimensionId)
            }
        });
        if(tempArray.length>0){
            ImsDialog.open({title:"专用标签管理",message:'确定删除该标签维度？'}).then(function (a) {
                if(a=="ok"){
                    TagsManager.specialDimensionsDelete(tempArray).then(function (result) {
                        if(result.data.c=="s"){
                            $state.reload();
                        }
                    })
                }
            })
        } else {
            return false;
            //ImsDialog.open({title:"专用标签管理",message:"没有可删除的数据。"}).then(function (a) {})
        }
    }



    }]);

